using Microsoft.Win32;

namespace TeamTasker.Utils
{
    /// <summary>
    /// Registry operations.
    /// </summary>
    public sealed class RegistryUtils
    {
        /// <summary>
        /// Retrieves a subkey value for the specified registry key
        /// </summary>
        /// <param name="key">Reference to valid registry key</param>
        /// <param name="path">Subkey path</param>
        /// <param name="name">Subkey value name</param>
        /// <returns>Subkey string value, or Nothing if value (key) does not exist </returns>
        /// <remarks></remarks>
        public static object GetValue(RegistryKey key, string path, string name)
        {
            return GetValue(key, path, name, RegistryValueKind.Unknown);
        }

        public static object GetValue(RegistryKey key, string path, string name, RegistryValueKind type)
        {
            RegistryKey subkey = null;
            try
            {
                //Open subkey
                subkey = key.OpenSubKey(path);
                if (subkey == null)
                {
                    return null;
                }
                //Check value type
                if (type != RegistryValueKind.Unknown)
                {
                    if (subkey.GetValueKind(name) != type)
                    {
                        return null;
                    }
                }
                return subkey.GetValue(name);
            }
            finally
            {
                //Close subkey
                if (subkey != null)
                {
                    subkey.Close();
                }
            }
        }

        public static void SetValue(RegistryKey key, string path, string name, object value)
        {
            SetValue(key, path, name, value, RegistryValueKind.Unknown);
        }

        public static void SetValue(RegistryKey key, string path, string name, object value, RegistryValueKind type)
        {
            RegistryKey subkey = null;
            try
            {
                //Open existing or create new subkey
                subkey = key.CreateSubKey(path);
                if (subkey != null)
                {
                    if (type == RegistryValueKind.Unknown)
                    {
                        subkey.SetValue(name, value);
                    }
                    else
                    {
                        subkey.SetValue(name, value, type);
                    }
                }
            }
            finally
            {
                //Close subkey
                if (subkey != null)
                {
                    subkey.Close();
                }
            }
        }

        public static void DeleteValue(RegistryKey key, string path, string name)
        {
            RegistryKey subkey = null;
            try
            {
                //Open subkey
                subkey = key.OpenSubKey(path, true);
                if (subkey != null)
                {
                    //Delete value
                    if (subkey.GetValue(name) != null)
                    {
                        subkey.DeleteValue(name);
                    }
                }
            }
            finally
            {
                //Close subkey
                if (subkey != null)
                {
                    subkey.Close();
                }
            }
        }
    }
}